#include "task.h"

namespace TinyRPC{
Task::Task(const TaskFunc& taskFunc, bool isCoroutine, std::string name)
: m_isCoroutine(isCoroutine){
    if(m_isCoroutine){
        m_coroutine = std::shared_ptr<Coroutine>(new Coroutine(taskFunc));
        m_coroutine->setName(name);
    }else{
        m_taskFunc = taskFunc;
    }
}

Task::Task(std::shared_ptr<Coroutine> coroutine)
: m_isCoroutine(true)
, m_coroutine(coroutine){

}

Task::Task()
: m_isCoroutine(false)
, m_coroutine(nullptr)
, m_taskFunc(nullptr){

}

// 
void Task::run() const{
    if(m_isCoroutine){
        // 确保是有主线程 来调用协程
        m_coroutine->makeSureInMain();
        m_coroutine->resume();
    }else if(m_taskFunc){
        m_taskFunc();
    }
}



}